package com.et.u1.vm.result;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import com.et.core.goods.GoodsInfo;
import com.et.u1.databinding.FragmentResultBinding;
import com.et.u1.util.DataKt;
import com.et.u1.vm.base.BaseFragment;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

public class ResultFragment extends BaseFragment<FragmentResultBinding, ResultViewModel> {

    private Timer timer;
    private int num;
    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                num--;
                Log.d("time", String.valueOf(num));
                if (num == 0) {
                    FragmentManager manager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
                    for (int i = 0; i < manager.getBackStackEntryCount() - 2; i++) {
                        manager.popBackStack();
                    }
                }
            } else if (msg.what == 2) {
                num = 15;
            }
        }
    };

    public static ResultFragment newInstance() {
        return new ResultFragment();
    }

    @NotNull
    @Override
    public FragmentResultBinding provideViewBinding(@NotNull LayoutInflater inflater, @org.jetbrains.annotations.Nullable ViewGroup container, @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return FragmentResultBinding.inflate(inflater, container, false);
    }

    @NotNull
    @Override
    public ResultViewModel provideViewModel() {
        return new ViewModelProvider(this).get(ResultViewModel.class);
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void initEvent() {

        if ((DataKt.getSize().y) - 800 <= 230) {
            if (binding.view4 != null && binding.textView4 != null && binding.imageView5 != null) {
                binding.view4.setVisibility(View.GONE);
                binding.textView4.setVisibility(View.GONE);
                binding.imageView5.setVisibility(View.GONE);
            }
        }

        GoodsInfo goodsInfo = mainModel.getPurchasedGoodsInfo();
        if (goodsInfo != null && binding.name != null && binding.price != null & binding.image != null) {
            binding.name.setText(goodsInfo.getName());
            binding.balance.setText(String.format("￥%.2f", (goodsInfo.getChannel().getPrice() / 100f)));
//            binding.price.setText(String.format("￥%.2f", (goodsInfo.getChannel().getPrice() * goodsInfo.getDiscount() / 10000f)));
            binding.channel.setText(goodsInfo.getChannel().getLabel());
            binding.image.setImageURI(Uri.parse(goodsInfo.getImage()));
        }
    }

    private void timeClocks() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(1);
            }
        }, 1000, 1000);
    }

    @Override
    public void onResume() {
        super.onResume();
        num = 15;
        timeClocks();
    }


    @Override
    public void onPause() {
        super.onPause();
        timer.cancel();
    }
}